import { _decorator, Camera, CCInteger, Component, EventTouch, math, Node, TiledLayer, TiledMap, UITransform, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('HexMap')
export class HexMap extends Component {
    private _tempV3 : Vec3;
    @property(TiledLayer)
    tiledlayer : TiledLayer = null;
    @property(Node)
    red : Node = null;
    public mainCamera : Camera = null;
    startPos : Vec3 = new Vec3(0, 0, 0);
    
    private offsetY : number = 0;
    @property(CCInteger)
    width : number = 200;
    @property(CCInteger)
    height : number = 173;

    //tieldmap的x,y数量
    private xNum : number = 50;
    private yNum : number = 50;
    //cocos坐标系的起点坐标x,y
    private csx : number = 0;
    private csy : number = 0;
    //tiledmap与cocos坐标系的转换偏移
    private ddx : number = 0;
    private ddy : number = 0;
    onLoad(): void {
        this._tempV3 = new Vec3();
        this.mainCamera = this.node.parent.getComponentInChildren(Camera);
        // this.tiledlayer = this.node.children[0].getComponent(TiledLayer);

        let sss = this.tiledlayer.getPositionAt(-1, -2);
        console.log("sss ", sss);

        let s = this.tiledlayer.getPositionAt(0, 0);
        let s1 = this.tiledlayer.getPositionAt(1, 0);

        console.log("s ", s,s1);

        console.log("offsetY ", -1%2);
        
        this.initTiledMap();

        let c0 = this.tiled2Cocos(0, 0);
        let c1 = this.tiled2Cocos(0, 1);
        let c2 = this.tiled2Cocos(-1, 2);
        console.log("c ", c0,c1,c2);

    }

    start() {
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this)
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this)
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this)
    }

    onTouchStart(et: EventTouch) {
        let touchPos = et.getLocation().toVec3();
        let worldPos = this.mainCamera.screenToWorld(touchPos, this._tempV3);
        let localPos = this.node.getComponent(UITransform).convertToNodeSpaceAR(worldPos);
        this.red.setPosition(localPos.x, localPos.y , 0);
        let tiledPos = this.cocos2Tiled(localPos.x, localPos.y);
        console.log("tiled ", tiledPos.x, tiledPos.y);
        
    }

    onTouchMove(et: EventTouch) {
        
    }

    onTouchEnd(et: EventTouch) {
        
    }

    // ccc(x:number, y:number){
    //     let point = new Vec2(x, -y);
    //     let wval = this.width/4*3;
    //     let w_num = x / wval;
    //     let w_int = Math.floor(w_num);
    //     let w_float = w_num - w_int;

    //     let h = y / 173;
    //     console.log(w_num, h);
    //     if (w_float <= 1/3) {
    //         if (w_int % 2 == 0) {
    //             console.log("坐标 ",w_int, Math.round(h));
    //         }else{
    //             console.log("坐标 ",w_int, Math.floor(h));
    //         }
    //     }else if (w_float <= 2/3) {
    //         if (w_int % 2 == 0) {
    //             //左边的格子r_int,h
    //             let hl = Math.round(h);
    //             let leftPos = this.tiledlayer.getPositionAt(w_int, hl);
    //             //tiled坐标转cocos
    //             leftPos = leftPos.subtract2f(0, this.offsetY);
    //             let leftDis = Vec2.squaredDistance(leftPos, point);
    //             //右边格子r_int+1,hr
    //             let hr = Math.floor(h);
    //             let rightPos = this.tiledlayer.getPositionAt(w_int+1, hr);
    //             rightPos = rightPos.subtract2f(0, this.offsetY);
    //             let rightDis = Vec2.squaredDistance(rightPos, point);
    //             if (leftDis < rightDis) {
    //                 console.log("left ", w_int, hl);
    //             }else{
    //                 console.log("right ", w_int+1, hr);
    //             }
    //         }else{
    //             //左边的格子r_int,h
    //             let hl = Math.floor(h);
    //             console.log("坐标x ",w_int, w_int+1 , hl);
    //             let leftPos = this.tiledlayer.getPositionAt(w_int, hl);
    //             leftPos = leftPos.subtract2f(0, this.offsetY);
    //             console.log("leftPos ", leftPos);
    //             let leftDis = Vec2.squaredDistance(leftPos, point);
    //             //右边格子r_int+1,hr
    //             let hr = Math.round(h);
    //             let rightPos = this.tiledlayer.getPositionAt(w_int+1, hr);
    //             rightPos = rightPos.subtract2f(0, this.offsetY);
    //             console.log("rightPos ", rightPos);
    //             let rightDis = Vec2.squaredDistance(rightPos, point);
    //             if (leftDis < rightDis) {
    //                 console.log("left ", w_int, hl);
    //             }else{
    //                 console.log("right ", w_int+1, hr);
    //             }
    //         }
    //     }else{
    //         w_int = w_int + 1;
    //         if (w_int % 2 == 0) {
    //             console.log("坐标 ",w_int, Math.round(h));
    //         }else{
    //             console.log("坐标 ",w_int, Math.floor(h));  
    //         }
    //     }
    // }
    // /**按向量计算位置关系 */
    // ccd(x:number, y:number){
    //     let point = new Vec2(x, -y);
    //     let wval = this.width/4*3;
    //     let w_num = x / wval;
    //     let w_int = Math.floor(w_num);
    //     let w_float = w_num - w_int;

    //     let h = y / this.height;
    //     console.log(w_num, h);
    //     if (w_float <= 1/3) {
    //         if (w_int % 2 == 0) {
    //             console.log("坐标 ",w_int, Math.round(h));
    //         }else{
    //             console.log("坐标 ",w_int, Math.floor(h));
    //         }
    //     }else if (w_float <= 2/3) {
    //         if (w_int % 2 == 0) {
    //             //左边的格子r_int,h
    //             let hl = Math.round(h);
    //             //右边格子r_int+1,hr
    //             let hr = Math.floor(h);
    //             if (hr == hl) {
    //                 let leftPos = this.tiledlayer.getPositionAt(w_int, hl + 1);
    //                 //tiled坐标转cocos
    //                 leftPos = leftPos.subtract2f(0, this.offsetY);
    //                 let rightPos = this.tiledlayer.getPositionAt(w_int+1, hr - 1);
    //                 //tiled坐标转cocos
    //                 rightPos = rightPos.subtract2f(0, this.offsetY);
    //                 //向量ab
    //                 let ab = rightPos.subtract(leftPos);
    //                 //向量叉乘
    //                 let val = Vec2.cross(ab, point.subtract(leftPos));
    //                 if (val > 0) {
    //                     console.log("left ", w_int, hl);
    //                 }else{
    //                     console.log("right ", w_int+1, hr);
    //                 }
    //             }else{
    //                 let leftPos = this.tiledlayer.getPositionAt(w_int, hl - 1);
    //                 //tiled坐标转cocos
    //                 leftPos = leftPos.subtract2f(0, this.offsetY);
    //                 let rightPos = this.tiledlayer.getPositionAt(w_int+1, hr + 1);
    //                 //tiled坐标转cocos
    //                 rightPos = rightPos.subtract2f(0, this.offsetY);
    //                 //向量ab
    //                 let ab = rightPos.subtract(leftPos);
    //                 //向量叉乘
    //                 let val = Vec2.cross(ab, point.subtract(leftPos));
    //                 if (val <= 0) {
    //                     console.log("left ", w_int, hl);
    //                 }else{
    //                     console.log("right ", w_int+1, hr);
    //                 }
    //             }
    //         }else{
    //             //左边的格子r_int,h
    //             let hl = Math.floor(h);
    //             //右边格子r_int+1,hr
    //             let hr = Math.round(h);
    //             if (hr == hl) {
    //                 let leftPos = this.tiledlayer.getPositionAt(w_int, hl - 1);
    //                 leftPos = leftPos.subtract2f(0, this.offsetY);        
    //                 let rightPos = this.tiledlayer.getPositionAt(w_int+1, hr + 1);
    //                 rightPos = rightPos.subtract2f(0, this.offsetY);                
    //                 //向量ab
    //                 let ab = rightPos.subtract(leftPos);
    //                 //向量叉乘
    //                 let val = Vec2.cross(ab, point.subtract(leftPos));
    //                 if (val > 0) {
    //                     console.log("right ", w_int+1, hr);
    //                 }else{
    //                     console.log("left ", w_int, hl);
    //                 }
    //             }else{
    //                 let leftPos = this.tiledlayer.getPositionAt(w_int, hl + 1);
    //                 leftPos = leftPos.subtract2f(0, this.offsetY);        
    //                 let rightPos = this.tiledlayer.getPositionAt(w_int+1, hr - 1);
    //                 rightPos = rightPos.subtract2f(0, this.offsetY);                
    //                 //向量ab
    //                 let ab = rightPos.subtract(leftPos);
    //                 //向量叉乘
    //                 let val = Vec2.cross(ab, point.subtract(leftPos));
    //                 if (val > 0) {
    //                     console.log("left ", w_int, hl);
    //                 }else{
    //                     console.log("right ", w_int+1, hr);
    //                 }
    //             }
    //         }
    //     }else{
    //         w_int = w_int + 1;
    //         if (w_int % 2 == 0) {
    //             console.log("坐标 ",w_int, Math.round(h));
    //         }else{
    //             console.log("坐标 ",w_int, Math.floor(h));  
    //         }
    //     }
    // }
    
    tiledStartPos():Vec2{
        return new Vec2(-3675, 4220.5);
    }

    initTiledMap(){
        //  计算起点坐标
        //  左边界 = 宽/2 = 7550/2 = -3775
        //  起点x =  左边界 + tw/2 = -3775 + 200/2 = -3675
        //  上空隙 = 高 - 50*th = 8686 - 50*173 = 36
        //  上边界 = 高/2 - 上空隙 = 8686/2 - 36 = 4307
        //  起点y = 上边界 - th/2 = 4307 - 173/2 = 4220.5
        //  起点坐标 = -3675, 4220.5
        let size = this.tiledlayer.node.getComponent(UITransform).contentSize;
        let left = size.width/2;
        let leftx = -left + this.width/2;
        let topOffset = size.height - this.height * this.yNum;
        let top = size.height/2 - topOffset;
        let topx = top - this.height/2;
        //tiledmap坐标系的起点坐标
        let tile0 = this.tiledlayer.getPositionAt(0, 0);
        this.csx = leftx;
        this.csy = topx;
        this.ddx = leftx - tile0.x;
        this.ddy = topx - tile0.y;
    }

    tiled2Cocos(r:number, n:number):Vec2{
        let tilePos = this.tiledlayer.getPositionAt(r, n);
        let x = tilePos.x + this.ddx;
        let y = tilePos.y + this.ddy;
        return new Vec2(x, y);
    }

    /**
     * cocos坐标转tiled坐标
     * @param x 
     * @param y 
     */
    cocos2Tiled(x:number, y:number) : Vec2{
        let tx = 0, ty = 0;
        let point = new Vec2(x, y);
        let wval = this.width / 4 * 3;
        let w_num = (x - this.csx) / wval;
        let w_int = Math.floor(w_num);
        //x小于0时计算可能有问题
        if (w_int < 0 ) {
            w_int = 0;
        }
        let w_float = w_num - w_int;
        let h = (this.csy - y) / this.height;
        if (w_float <= 1/3) {
            if (w_int % 2 == 0) {
                tx = w_int;
                ty = Math.round(h);
            }else{
                tx = w_int;
                ty = Math.floor(h);
            }
        }else if (w_float <= 2/3) {
            if (w_int % 2 == 0) {
                //左边的格子r_int,h
                let hl = Math.round(h);
                //右边格子r_int+1,hr
                let hr = Math.floor(h);
                if (hr == hl) {
                    let leftPos = this.tiled2Cocos(w_int, hl + 1);
                    let rightPos = this.tiled2Cocos(w_int+1, hr - 1);
                    //向量ab
                    let ab = rightPos.subtract(leftPos);
                    //向量叉乘
                    let val = Vec2.cross(ab, point.subtract(leftPos));
                    if (val > 0) {
                        tx = w_int;
                        ty = hl;
                    }else{
                        tx = w_int + 1;
                        ty = hr;
                    }
                }else{
                    let leftPos = this.tiled2Cocos(w_int, hl - 1);
                    let rightPos = this.tiled2Cocos(w_int+1, hr + 1);
                    //向量ab
                    let ab = rightPos.subtract(leftPos);
                    //向量叉乘
                    let val = Vec2.cross(ab, point.subtract(leftPos));
                    if (val <= 0) {
                        tx = w_int;
                        ty = hl;
                    }else{
                        tx = w_int + 1;
                        ty = hr;
                    }
                }
            }else{
                //左边的格子r_int,h
                let hl = Math.floor(h);
                //右边格子r_int+1,hr
                let hr = Math.round(h);
                console.log("坐标hl hr ",hl, hr);
                
                if (hr == hl) {
                    let leftPos = this.tiled2Cocos(w_int, hl - 1);    
                    let rightPos = this.tiled2Cocos(w_int+1, hr + 1);    
                    console.log("leftPos rightPos ",leftPos, rightPos);          
                    //向量ab
                    let ab = rightPos.subtract(leftPos);
                    //向量叉乘
                    let val = Vec2.cross(ab, point.subtract(leftPos));
                    console.log("val ",val);
                    if (val == undefined) {
                        console.log("val ==> undefined");
                    }
                    if (val > 0) {
                        tx = w_int + 1;
                        ty = hr;
                    }else{
                        tx = w_int;
                        ty = hl;
                    }
                }else{
                    let leftPos = this.tiled2Cocos(w_int, hl + 1);      
                    let rightPos = this.tiled2Cocos(w_int+1, hr - 1);               
                    //向量ab
                    let ab = rightPos.subtract(leftPos);
                    //向量叉乘
                    let val = Vec2.cross(ab, point.subtract(leftPos));
                    if (val > 0) {
                        tx = w_int;
                        ty = hl;
                    }else{
                        tx = w_int + 1;
                        ty = hr;
                    }
                }
            }
        }else{
            w_int = w_int + 1;
            if (w_int % 2 == 0) {
                tx = w_int;
                ty = Math.round(h);
            }else{
                tx = w_int;
                ty = Math.floor(h);
            }
        }
        return new Vec2(tx, ty);
    }
    
}


